package com.citrix.gs.util;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.citrix.gs.kit.CommonKit;
import com.citrix.gs.util.beacon.BeaconCI;
import com.citrix.gs.util.beacon.BeaconServer;
import com.citrix.gs.util.common.CIInfo;
import com.citrix.gs.util.common.Config;

/*
 * Clear tool's detect result form beacon database
 */

public class ClearDetectResult {
    
    private static Logger logger = LoggerFactory.getLogger(ClearDetectResult.class);
    
    private CIInfo ciInfo;
    private BeaconCI ci = null;
    private BeaconServer server = null;
    public BeaconCI getCi() {
        return ci;
    }

    private String mongodbUrl;
    
    public ClearDetectResult(String mongodbUrl){
        this.mongodbUrl = mongodbUrl;
    }
    
    public ClearDetectResult(CIInfo ciInfo, String mongodbUrl) {
        this(mongodbUrl);
        this.ciInfo = ciInfo;
        this.ci = new BeaconCI(ciInfo);
    }
    
    public ClearDetectResult(String beaconRestUrl, String mongodbUrl) {
        this(mongodbUrl);
        try {
            this.server = new BeaconServer(beaconRestUrl);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    
    public ClearDetectResult(String beaconRestUrl, CIInfo ciInfo, String mongodbUrl)  {
        this(ciInfo, mongodbUrl);
        try {
            this.server = new BeaconServer(beaconRestUrl);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    
    /*
     * Delete revisions of repository to make suer specified error code has been removed
     */
    public void deleteErrorCode(String repository, String errorCode) throws IOException {
        
        List<String> revsions = server.getRevisionsWithError(repository, errorCode);
        
        revsions.forEach(new Consumer<String>() {
            @Override
            public void accept(String revision) {
                if(!deleteRevision(repository, revision)){
                    logger.error("Fail to remove {} in {}", revision, repository);
                }
            }
        });
    }
    
    public boolean deleteLatestRevision(String repository) {
        
        String latestRevision = ci.getLastSrcInfo(repository).getRevisionNo();
        
        return deleteRevision(repository, latestRevision);
    }
    
    public boolean deleteRevision(String repository, String revision) {
        
        String pythonScript = Config.me().getProperty("db.admin.delete_revision", "python /Users/yzhou-citrix/Perforce/beaconci/DB_Management_Scripts/delete_revision.py");
        String cmd = String.format("%s %s %s %s", pythonScript, mongodbUrl, repository, revision);
        
        logger.info("Run cmd: {}", cmd);
        String output = CommonKit.runCmd(cmd);
        
        if(output.startsWith("Error")) {
            logger.error(output);
            return false;
        } else {
            logger.info(output);
            return true;
        }
    }

    public static void main(String[] args) {
        
        
        Config conf = Config.me();
        
        ClearDetectResult cdr = new ClearDetectResult(conf.getTestBeaconRestURL(),
                conf.getTestCI(), conf.getTestMongodbURL());
        
        try {
            cdr.deleteErrorCode("ShareFile-iOS_App-master", "2012");
            cdr.deleteErrorCode("ShareFile-iOS_App-Develop", "2012");
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        

        /*
        String [][] revisions = {
                {"RfiOS-RfiOS-5.9.4","337460"}         
        };
        
        for(String [] revision : revisions) {
            logger.info("Delete revision {} from {}", revision[1], revision[0]);
            cdr.deleteRevision(revision[0], revision[1]);
        }*/
        
        logger.info("Over");

    }

}
